<template>
  <div class="driverList-wrapper">
    <el-dialog title="设置司机" class="tableDialog" width="800px" :visible.sync="drivervisible">
      <div class="headerMultiple">
        <section class="model-name">
          <span>{{truckNum}},共{{page.total}}个司机</span>
        </section>
          <section>
              <el-button  v-if="hasPermission('truck:truckDriver:add')" type="primary"
                          size="small" @click="add">新增</el-button>
          </section>
      </div>

      <el-table size="mini" v-loading="loading" :data="tableData" style="width: 100%; margin-top: 10px" border
        :header-cell-style="{
        'background-color': '#eeeeee !important',
      }" :row-class-name="tableRowClassName">
        <el-table-column type="index" label="序号" width="60" align="center">
          <template slot-scope="scope">
            <span>{{
            (page.pageNo - 1) * page.pageSize + scope.$index + 1
          }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="driver.name" label="司机姓名" sortable="name" width="120">
          <template slot-scope="scope">
            <section>
            {{ scope.row.driver.name }}
          <span v-if="scope.row.isUsed === 1" class="default"
          >常用</span>
          </section>
          </template>
        </el-table-column>
        <el-table-column prop="driver.phone" label="手机号" width="130">
        </el-table-column>
        <el-table-column prop="driver.idcard" label="身份证号" width="200">
        </el-table-column>
        <el-table-column prop="address" label="操作" width="80">
          <template slot-scope="scope">
            <el-link type="primary" :underline="false" v-if="hasPermission('truck:truckDriver:edit')" @click="edit(scope.row)">修改
            </el-link>
            <el-link type="primary" :underline="false" v-if="hasPermission('truck:truckDriver:del') && scope.row.isUsed === 0" @click="del(scope.row.id)">删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @current-change="handleCurrentChange" :page-size="page.pageSize" :current-page="page.pageNo"
        layout="total,prev, pager, next" :total="page.total">
      </el-pagination>
    </el-dialog>
    <DriverFrom ref="driverFrom" @refreshDataList="refreshList(page, 'createDate', 'asc')"></DriverFrom>
  </div>
</template>
<script>
  import DriverFrom from './driverFrom'
  export default {
    components: {
      DriverFrom
    },
    data () {
      return {
        drivervisible: false,
        ShowCloum: {
          createDate: ''
        },
        options: [{
          label: '上次操作时间',
          key: '2'
        }, {
          label: '创建时间',
          key: 'createDate'
        }],
        queryForm: {
          truck: {
            id: ''
          }
        },
        truckId: '',
        truckNum: '',
        page: {
          pageSize: 10,
          pageNo: 1,
          total: 0
        },
        tableData: [{
          driver: {},
          truck: {}
        }],
        orderBy: {
          name: 'update_date',
          method: 'desc'
        },
        loading: false
      }
    },
    methods: {
      init (method, obj) {
        if (method === 'list') {
          this.drivervisible = true
          this.truckId = obj.id
          this.truckNum = obj.truckNumber
          this.refreshList(this.page)
        }
      },
      // 获取数据列表
      refreshList (params) {
        this.loading = true
        this.$http({
          url: '/truck/driver/driverList',
          method: 'get',
          params: {
            'pageNo': params.pageNo,
            'pageSize': params.pageSize,
            'truck.id': this.truckId
          }
        }).then(({
          data
        }) => {
          if (data.code === '200') {
            this.tableData = data.result.list
            this.page.total = data.result.count
            this.loading = false
          }
        })
      },
      editUse(row){
        this.$confirm('确定设置为常用司机?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((res) => {
          this.loading = true
          this.$http({
            url: '/truck/driver/editUse',
            method: 'post',
            params: {
              'id': row.id
            }
          }).then(({
            data
          }) => {
            if (data && data.success) {
              this.$message.success(data.message)
              this.refreshList(this.page, this.truckId)
            }
            this.loading = false
          })
        })
      },
      edit (row) {
        this.$refs.driverFrom.init('edit', row)
      },
      add () {
        this.$refs.driverFrom.init('add', this.truckId)
      },
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((res) => {
          this.loading = true
          this.$http({
            url: '/truck/driver/deleteRelation',
            method: 'delete',
            params: {
              'ids': ids
            }
          }).then(({
            data
          }) => {
            if (data && data.success) {
              this.$message.success(data.message)
              this.refreshList(this.page, this.truckId)
            }
            this.loading = false
          })
        })
      },
      handleCurrentChange (e) {
        this.page.pageNo = e
        this.refreshList(this.page, this.truckId)
      },
      tableRowClassName ({
        row,
        rowIndex
      }) {
        if (rowIndex % 2 === 1) {
          return 'warning-row'
        } else {
          return 'success-row'
        }
      }

    }
  }
</script>
<style lang="scss" scoped>
  .driverList-wrapper{
    ::v-deep.el-dialog{
        .header{
            display: flex;
            .model-name{
                flex: 1;
            }
        }
    }
  }
  ::v-deep .el-input--small .el-input__inner {
    height: 36px;
    line-height: 32px;
    width: 230px;
  }
  .default{
    color: #0000EE;
      margin-left: 20px;
  }

  .queryForm {
    position: relative;

    .but-container {
      top: 20px;
      margin-left: 35px;
    }
  }

  .right-but {
    float: right;
    margin-right: 10px;
    margin-top: -20px;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  ::v-deep .el-table .warning-row {
    background: #f7f7f7;
  }
  .el-pagination{
    padding-bottom: 24px;
  }
</style>
